package com.guaika.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guaika.reggie.common.ResultMessage;
import com.guaika.reggie.common.StringChars;
import com.guaika.reggie.entity.Employee;
import com.guaika.reggie.mapper.EmployeeMapper;
import com.guaika.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author GuaiKa
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>  implements EmployeeService{


    @Override
    public ResultMessage login(HttpServletRequest request, Employee employee) {
        //加密密码
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //根据用户名查询用户信息
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one = this.getOne(lambdaQueryWrapper);
        //判断用户是否存在
        if(one == null){
            return ResultMessage.error(StringChars.NONENTITY_ERROR.getStr());
        }
        //比对密码
        if(!password.equals(one.getPassword())){
            return ResultMessage.error(StringChars.ACCOUNT_ERROR.getStr());
        }
        //判断用户状态
        if(one.getStatus() == 0){
            return ResultMessage.error(StringChars.LOGIN_STATUS_ERROR.getStr());
        }
        //存入session
        request.getSession().setAttribute(StringChars.EMPLOYEE.getStr(), one.getId());
        return ResultMessage.success(one);
    }


    @Override
    public ResultMessage<String> employeeSave(HttpServletRequest request, Employee employee) {
        //获取Session中的用户id
        Long userId = (Long) request.getSession().getAttribute(StringChars.EMPLOYEE.getStr());
        //设置默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex(StringChars.DEFAULT_PASSWORD.getStr().getBytes()));
        //保存至数据库
        this.save(employee);
        return ResultMessage.success(StringChars.SAVE_MESSAGE.getStr());
    }
    @Override
    public ResultMessage<Page> employeePage(int page, int pageSize, String name) {
        //分页构造器
        Page employeePage = new Page(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName, name);
        //添加排序条件
        lambdaQueryWrapper.orderByDesc(Employee::getCreateTime);
        //执行查询
        this.page(employeePage,lambdaQueryWrapper);
        return ResultMessage.success(employeePage);
    }

    @Override
    public ResultMessage<String> employeeUpdate(Employee employee, HttpServletRequest request) {
        //获取session中的当前登录用户id
        Long id = (Long) request.getSession().getAttribute(StringChars.EMPLOYEE.getStr());
        employee.setUpdateUser(id);
        this.updateById(employee);
        return ResultMessage.success(StringChars.UPDATE_SUCCESS.getStr());
    }

    @Override
    public ResultMessage<Employee> employeeQueryById(Long id) {
        Employee employee = this.getById(id);
        if(employee!=null){
            return ResultMessage.success(employee);
        }
        return ResultMessage.error(StringChars.QUERY_USER_ERROR.getStr());
    }
}
